<%--
  Created by IntelliJ IDEA.
  User: 廉壮
  Date: 2023/5/16
  Time: 18:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
  <meta charset="UTF-8">
  <title>欢迎注册</title>
</head>
<script src="${ctx}/static/js/jquery.js"></script>
<style>
  body {
    background: #353f42;
  }

  * {
    padding: 0;
    margin: 0;
  }

  .main {
    margin: 0 auto;
    padding-left: 25px;
    padding-right: 25px;
    padding-top: 15px;
    width: 350px;
    height: 400px;
    background: #FFFFFF;
    /*以下css用于让登录表单垂直居中在界面,可删除*/
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -175px;
    margin-left: -175px;
  }

  .title {
    width: 100%;
    height: 40px;
    line-height: 40px;
  }

  .title span {
    font-size: 18px;
    color: #353f42;
  }

  .title-msg {
    width: 100%;
    height: 64px;
    line-height: 64px;
  }

  .title:hover {
    cursor: default;
  }

  .title-msg:hover {
    cursor: default;
  }

  .title-msg span {
    font-size: 12px;
    color: #707472;
  }

  .input-content {
    width: 100%;
    height: 120px;
  }

  .input-content input {
    width: 330px;
    height: 40px;
    border: 1px solid #dad9d6;
    background: #ffffff;
    padding-left: 10px;
    padding-right: 10px;
  }

  .enter-btn {
    width: 350px;
    height: 40px;
    color: #fff;
    background: #0bc5de;
    line-height: 40px;
    text-align: center;
    border: 0px;
  }

  #upload {
    background-color: #3897f0;
    color: #fff;
    border: none;
    border-radius: 5px;
    padding: 5px 10px;
    font-size: 14px;
    cursor: pointer;
    margin-left: auto;
  }

  .foor {
    width: 100%;
    height: auto;
    color: #9b9c98;
    font-size: 12px;
    margin-top: 20px;
  }

  .enter-btn:hover {
    cursor: pointer;
    background: #1db5c9;
  }

  .foor div:hover {
    cursor: pointer;
    color: #484847;
    font-weight: 600;
  }

  .left {
    float: left;
  }

  .right {
    float: right;
  }

  .bbb {
    text-decoration: none;
    color: white;
  }

  #usernameMsg{
    color: red;
  }

  #imageFile{
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }

  #image-div{
    float: right;
    position: relative;
    top: -34px;
    left: -29px;
  }

</style>

<body>

<div class="main">

  <div class="title">
    <span>注册界面</span>
    <a href="#"><input type="button" src="#" value="上传头像" id="upload" ></a>

  </div>
  <div id="image-div">
    <img src="" id="imageFile">
  </div>

  <div class="title-msg">
    <span>请输入正确的用户名和密码</span>
  </div>
  <form class="login-form" action="${ctx}/user/add">
    <!--输入框-->
    <div class="input-content">
      <!--autoFocus-->
      <input name="image" class="image" id="image" style="display: none">
      <div>
        <input type="text" placeholder="用户名" value="" id="username" name="username" required
               maxlength="32" />
      </div>
      <div style="margin-top: 16px">
        <input type="password" autocomplete="off" placeholder="密码" id="password" value="" name="password"
               required maxlength="32" />
      </div>
      <div style="margin-top: 16px">
        <input type="password" autocomplete="off" placeholder="确认密码" id="passwordCheck" value="" name="passwordCheck"
               required maxlength="32" />
        <p id="usernameMsg"></p>
      </div>
      <div style="margin-top: 16px">
        <input type="file" id="file" name="file" style="display: none"/>
      </div>
    </div>
    <!--登入按钮-->
  </form>
  <div style="text-align: center;margin-top: 60px;">
    <a class="bbb" href="#"><button type="submit" class="enter-btn" id="save">确定注册</button></a>
  </div>
  <div class="foor">
    <label style="color: red;position:relative;left: 100px;"></label>
    <div class="right"><span>已有账号？<a href="${ctx}/login.jsp">前往登录</a> </span></div>
  </div>
</div>

<script>
  $(function () {


    $("#username").blur(function (){
      let name = $(this).val();
      $.get("${ctx}/user/usernameCheck?username=" + name,function (e){
        if(e != "ok"){
          $("#usernameMsg").text(e);
          $("#username").focus();
        }else{
          $("#usernameMsg").text("");
        }
      })
    })


    $("#passwordCheck").blur(function () {
      let password = $("#password").val();
      let passwordCheck = $(this).val();
      if(passwordCheck!=password){
        $("#usernameMsg").text("两次密码不一致");
      }else {
        $("#usernameMsg").text("");
      }
    })

    $("#save").click(function () {
      $.get("${ctx}/user/add",$("form").serialize(), function (e) {
        if(e=="ok"){
          alert("注册成功");
          location.href = "${ctx}/login.jsp";
        }else {
          alert("注册失败" + e);
        }
      })
      return false;
    })


    $("#upload").on("click",function () {
      $("#file").click();
      return false;
    })

    $("#file").change(function () {
      $.ajax({
        url : "${ctx}/common/uploadOne",
        type : "POST",
        data:new FormData($("form")[0]),
        processData: false,		//是否将数据序列化成查询字符串，默认为true
        contentType: false,		//设置为false，否则默认是表单内容
        success:function (e) {
          console.log(e);
          if(e == "err"){
            alert("上传失败");
          }else{
            $("#imageFile").attr("src","${ctx}/common/getImage?image=" + e);
            $("#image").attr("value",e)
          }
        }
      })
    })
  })
</script>
</body>
</html>
